<?php

namespace app\home\controller;


use think\facade\App;
use think\facade\Request;

class UrlGroup extends Base {
    
    protected function initialize()
    {
        parent::initialize();
        $this->checkLogin();
    }
    
    /**
     * @desc 我的链接
     */
    public function index() {
        $params['pageSize']  = input('pageSize', 10);

        $data = model('UrlGroup', 'logic')->index($params, $this->userId);
        $this->assign($data);
        return view();
    }
    
    /**
     * @desc 添加分组
     */
    public function add() {
        return view();
    }
    
    /**
     * @desc 编辑分组
     */
    public function edit() {
        $groupId = input('id', 0);
        $data = model('UrlGroup', 'logic')->edit($groupId, $this->userId);
        $this->assign($data);
        return view();
    }
    
    /**
     * @desc 创建链接
     */
    public function operateAjax() {
        $params = [
            'id'          => Request::post('id', 0),
            'action'      => Request::post('action', 'add'),

            'groupName'   => Request::post('groupName'),
            'status'      => Request::post('status'),
            'description' => Request::post('description'),
        ];
        
        $logic = model('UrlGroup', 'logic');
        $data = [];
        try {
            $data = $logic->operateAjax($params, $this->userId);
        } catch (\Exception $e) {
            $logic->errorCode = EC_FAILURE;
            $logic->message = $e->getMessage();
        }
        return send_response($data, $logic->errorCode, $logic->message);
    }
}
